package com.lenovo.newsapp.adapter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.newsapp.R;
import com.lenovo.newsapp.activity.CommentActivity;
import com.lenovo.newsapp.activity.LoginActivity;
import com.lenovo.newsapp.model.Comment;
import com.lenovo.newsapp.util.Const;
import com.lenovo.newsapp.util.PreferenceUtil;
import com.worklight.wlclient.api.WLClient;
import com.worklight.wlclient.api.WLFailResponse;
import com.worklight.wlclient.api.WLProcedureInvocationData;
import com.worklight.wlclient.api.WLResponse;
import com.worklight.wlclient.api.WLResponseListener;

public class CommentAdapter extends BaseArrayAdapter<Comment> {

	private static final String TAG = CommentAdapter.class.getSimpleName();

	private ViewHolder holder;
	
	private CommentActivity context;
	
	private final Handler handler = new Handler();

	public CommentAdapter(CommentActivity context, ArrayList<Comment> list) {
		super(context, list, null);
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.item_comment, null);
			holder = new ViewHolder();
			convertView.setTag(holder);
			holder.username = (TextView) convertView
					.findViewById(R.id.comment_username_tv);
			holder.time = (TextView) convertView
					.findViewById(R.id.comment_time_tv);
			holder.content = (TextView) convertView
					.findViewById(R.id.comment_content_tv);
			holder.likeNumber = (TextView) convertView
					.findViewById(R.id.comment_like_number_tv);
			holder.likeBtn = (LinearLayout) convertView
					.findViewById(R.id.comment_like_ll);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final Comment item = (Comment) getItem(position);
		holder.username.setText(item.getUserName());
		holder.time.setText(item.getLastUpdateTime().substring(0,19));
		holder.content.setText(item.getCommentContent());
		holder.likeNumber.setText(item.getUpsNumber()+"");
		holder.likeBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(item.getIsLike() == 0){
					
					String adapterName = "NewsAppAdapter";
					String procedureName = "commentsLike";
					
					Log.v(TAG, item.getCommentId()+"");
					WLProcedureInvocationData invocationData = new WLProcedureInvocationData(
							adapterName, procedureName);
					Object[] parameters = new Object[] {item.getCommentId()+"", PreferenceUtil.getString("username", "")};
					invocationData.setParameters(parameters);
					
					LoginActivity.client = WLClient.getInstance();
					LoginActivity.client.invokeProcedure(invocationData,
							new WLResponseListener() {
						
						public void onSuccess(WLResponse response) {
							final JSONObject jsonObj = response.getResponseJSON();
							Log.v(Const.DEBUG, "onSuccess--dataJson = " + jsonObj);
							
							handler.post(new Runnable() {
								
								@Override
								public void run() {
									try {
										Log.v(Const.DEBUG, "onSuccess--dataJson = " + jsonObj.getInt("status_code"));
										if(jsonObj.getInt("status_code") == 0){
											Toast.makeText(context, R.string.like_success,Toast.LENGTH_LONG).show();
										}else{
											Toast.makeText(context, R.string.like_failure,Toast.LENGTH_LONG).show();
										}
										context.onHeaderLoad();
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
								
							});
							
						}
						
						public void onFailure(WLFailResponse response) {
							Log.v(Const.DEBUG, "onFailure--dataJson = " + response);
//								String responseText = response.getResponseText();
							handler.post(new Runnable() {
								
								@Override
								public void run() {
									Toast.makeText(context, R.string.like_failure,Toast.LENGTH_LONG).show();
								}
								
							});
						}
						
					});
				}else{
					Toast.makeText(context, R.string.already_like,Toast.LENGTH_SHORT).show();
				}
				
			}
		});

		return convertView;
	}

	static class ViewHolder {
		TextView username;
		TextView time;
		TextView content;
		TextView likeNumber;
		LinearLayout likeBtn;
	}

}
